package cn.fancy.sdk.common.utils;

import android.app.Activity;

public abstract class SdkAsyncTask<T>
{
	
	 public abstract Activity getOwnerActivity();
	 
	 private boolean isCancelled = false;
	 
	 public void cancel(boolean cancle)
	 {
		 this.isCancelled = cancle;
	 }
	 
	 private Thread thread = null;
	 
	 protected abstract T doInBackground();

	 protected abstract void onPostExecute(T obj);
	
	 protected abstract void onCancelled();
	
	 public void execute()
	 {
		
		
		thread = new Thread(new Runnable(){

			@Override
			public void run() 
			{
				
				final T obj = doInBackground();
				
				if(isCancelled == true){
					
					onCancelled();
					
					return;
					
				}
				
				if(getOwnerActivity() == null) return;
				
				getOwnerActivity().runOnUiThread(new Runnable(){

					@Override
					public void run() {
						
						onPostExecute(obj);
						
					}});
				
			}});
		
		 
		thread.start();
		
	}
	
}



